package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.drop.SetDrop;
import com.lineage.server.templates.L1DropMob;
import com.lineage.server.templates.L1Item;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DropMobTable {
  private static final Log _log = LogFactory.getLog(DropMobTable.class);
  
  private static DropMobTable _instance;
  
  public static DropMobTable get() {
    if (_instance == null)
      _instance = new DropMobTable(); 
    return _instance;
  }
  
  public void load() {
    Map<Integer, L1DropMob> droplists = allMobDropList();
    SetDrop setDrop = new SetDrop();
    setDrop.addDropMob(droplists);
  }
  
  private Map<Integer, L1DropMob> allMobDropList() {
    Map<Integer, L1DropMob> _droplistMob = new HashMap<>();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `w_全服掉落`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int itemId = rs.getInt("itemid");
        int min = rs.getInt("min");
        int max = rs.getInt("max");
        int chance = rs.getInt("chance");
        int week = rs.getInt("限制星期");
        int starttime = rs.getInt("几点开始");
        int unitytime = rs.getInt("几点结束");
        String note = rs.getString("note");
        if (!note.contains("=>"))
          updata_name(itemId); 
        if (check_item(itemId)) {
          L1DropMob drop = new L1DropMob();
          drop.setMin(min);
          drop.setMax(max);
          drop.setChance(chance);
          drop.setweek(week);
          drop.setStarttime(starttime);
          drop.setunitytime(unitytime);
          _droplistMob.put(Integer.valueOf(itemId), drop);
        } 
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入全部怪物掉落:" + _droplistMob.size());
    return _droplistMob;
  }
  
  private static void updata_name(int itemid) {
    Connection cn = null;
    PreparedStatement ps = null;
    String itemname = ItemTable.get().getTemplate(itemid).getName();
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("UPDATE `w_全服掉落` SET `note`=? WHERE `itemid`=?");
      int i = 0;
      ps.setString(++i, "全怪掉落->" + itemname);
      ps.setInt(++i, itemid);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private boolean check_item(int itemId) {
    L1Item itemTemplate = ItemTable.get().getTemplate(itemId);
    if (itemTemplate == null) {
      _log.info("全部怪物掉落错误物品->" + itemId);
      return false;
    } 
    return true;
  }
}
